/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.exception.RRException;
import io.renren.modules.store.service.TStoreSellerService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.entity.SysUserRoleEntity;
import io.renren.modules.sys.service.SysUserRoleService;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Controller公共组件
 *
 * @author Mark sunlightcs@gmail.com
 */
public abstract class AbstractController {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private SysUserRoleService sysUserRoleService;
	
	protected SysUserEntity getUser() {
		return (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
	}

	protected Long getUserId() {
		return getUser().getUserId();
	}

	protected Long getSeller(){
		if (request.getHeader("seller").equals("")){
			throw new RRException("系统出现异常,即将从新登录",410);
		}
		else if (request.getHeader("seller").equals("1")){
			throw new RRException("管理员无权限访问");
		}
		else {
			return Long.parseLong(request.getHeader("seller"));
		}
	}
	//用户角色
	protected List<Long> getRoleId(){
		List<SysUserRoleEntity> RoleIdList = sysUserRoleService.list(new QueryWrapper<SysUserRoleEntity>().select("role_id").eq("user_id", getUserId()));
		List<Long> roleList = new LinkedList<>();
		for (SysUserRoleEntity sysUserRoleEntity : RoleIdList) {
			roleList.add(sysUserRoleEntity.getRoleId());
		}
		return roleList;
	}
}
